<?php
namespace common\components\grid;

use yii\helpers\Html;
use yii\helpers\ArrayHelper;

class GridView2015 extends \kartik\grid\GridView {
	
	public $hover = true; //各行换色
	
	public $showFooter = false;
	
	public $pjax = true;
	
	public $showAdd = true;
	
	public $deleteAll = true;
	
	public $showToolbar = false;
	
	public $pager = [
		'firstPageLabel' => '首页',
		'lastPageLabel' => '尾页',
	];
	
    public $pjaxSettings = [
 	   'neverTimeout' => true 
	];
	public $toggleDataOptions = [ 
		'all' => [ 
			'icon' => 'resize-full',
			'label' => '显示所有',
			'class' => 'btn btn-default',
			'title' => '显示所有数据' 
		],
		'page' => [ 
			'icon' => 'resize-small',
			'label' => '分页显示',
			'class' => 'btn btn-default',
			'title' => '分页显示数据' 
		] 
	];
	
	public $export = [
		'header' => '',
	];
	
	public $title = '';
	
	public $enableButton = true;

	public function init() {
		$this->options = [
			'id' => 'grid2015'
		];
		$class_name = $this->dataProvider->query->modelClass;
		$model = new $class_name;
		
		if ($model->hasAttribute('is_deleted') && !f_get('rubbish')) {
			$deleteAll = Html::button('<i class="glyphicon glyphicon-remove"></i> 批量回收站', [
				'class' => 'btn btn-success',
				'style' => 'margin-left:20px;',
				'onclick' => '
	        		var keys = $("#grid2015").yiiGridView("getSelectedRows").length;
	        		if (keys > 0) {
						if (confirm("你确定要加入回收站吗？")) {
							$.ajax({
								url : "'.f_url(['common/rubbish-all']).'",
	        					data : {name:"'.base64_encode($class_name).'", pks:$("#grid2015").yiiGridView("getSelectedRows")},
	        					type : "post",
	        					success : function(data) {
									window.location.reload();
								}
							})
						}
					} else {
						alert("没有被选中的记录");
					}
	        	'
			]);
 	     } else {
// 			$deleteAll = Html::button('<i class="glyphicon glyphicon-remove"></i> 批量删除', [
// 				'class' => 'btn btn-success',
//				'style' => 'margin-left:20px;',
// 				'onclick' => '
// 	        		var keys = $("#grid2015").yiiGridView("getSelectedRows").length;
// 	        		if (keys > 0) {
// 						if (confirm("你确定要删除这条记录吗？")) {
// 							$.ajax({
// 								url : "'.f_url(['common/delete-all']).'",
// 	        					data : {name:"'.base64_encode($class_name).'", pks:$("#grid2015").yiiGridView("getSelectedRows")},
// 	        					type : "post",
// 	        					success : function(data) {
// 									window.location.reload();
// 								}
// 							})
// 						}
// 					} else {
// 						alert("没有被选中的记录");
// 					} 	        	'
// 			]);
                    $deleteAll = '';
		}
		
		$add = Html::a('添加', ['create'], ['class' => 'btn btn-success']);
		$included = Html::a('未收录热词', ['base-new-hot-word/index'], ['class' => 'btn btn-success','style' => 'margin-left:20px;']);
		$rubbish = 
			$model->hasAttribute('is_deleted') ? 
			f_get('rubbish') ? 
			Html::a('<i class="glyphicon glyphicon-trash"></i> 进入列表页', ['index'], ['class' => 'btn btn-success', 'style' => 'margin-left:20px;']) :
			Html::a('<i class="glyphicon glyphicon-trash"></i> 进入回收站', ['', 'rubbish' => 1], ['class' => 'btn btn-success', 'style' => 'margin-left:20px;']):
			'';
		!$this->showAdd && $add = '';
		!$this->deleteAll && $deleteAll = '';
		!$this->enableButton && $add = $deleteAll = $rubbish = '';
		if ($class_name == 'common\models\BaseHotWord'){
			$this->panel['before'] = isset($this->panel['before']) ? $add.$rubbish.$this->panel['before'] : $add.$deleteAll.$included.$rubbish;
		}else {
			$this->panel['before'] = isset($this->panel['before']) ? $add.$rubbish.$this->panel['before'] : $add.$deleteAll.$rubbish;	
		}
		
		$panel = [
			'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe">'.$this->title.'</i></h3>',
    		'type'=>'success',
		];
		$this->panel = ArrayHelper::merge($panel, $this->panel);
// 		if ($this->toolbar !== false) {
// 			$this->toolbar =  [
// 				['content'=>
// 					Html::button('<i class="glyphicon glyphicon-plus"></i>', ['type'=>'button', 'title'=>'Add Book', 'class'=>'btn btn-success', 'onclick'=>'alert("This will launch the book creation form.\n\nDisabled for this demo!");']) . ' '.
// 					Html::a('<i class="glyphicon glyphicon-repeat"></i>', [''], ['data-pjax'=>0, 'class' => 'btn btn-default', 'title'=>'kvgrid', 'Reset Grid'])
// 				],
// 				'{export}',
// 				'{toggleData}',
// 			];
// 		}
		if (!$this->showToolbar) $this->toolbar = false;
		parent::init();
	}	
}